using System;
using UnityEngine;

namespace Gj
{
    public static class LogTools
    {
        public static LogSettings settings;
        public static bool debug = true;

        static LogTools()
        {
            settings = LoadSettings();
        }
        
        private static LogSettings LoadSettings()
        {
            var loadedSettings = (LogSettings)Resources.Load("LogSettings", typeof(LogSettings));
            if (loadedSettings == null)
            {
                loadedSettings = ScriptableObject.CreateInstance<LogSettings>();
                Debug.LogError("LogSettings is not exist (Resources)");
            }

            return loadedSettings;
        }

        private static string Format(string c, string s)
        {
            return "<color=" + c + ">" + s + "</color>";
        }

        private static string Format(string c, string t, string s)
        {
            return Format(c, t + ": " + s);
        }

        private static string Format(string c, string m, string t, string s)
        {
            return Format(c, "[ " + m.ToUpper() + " ] " + t + ": " + s);
        }

        public static void Process(string s)
        {
            if (!settings.process || !debug) return;
            Debug.Log(Format("#55FF00", s));
        }

        public static void Process(string s, params object[] p)
        {
            if (!settings.process || !debug) return;
            Debug.LogFormat(Format("#55FF00", s), p);
        }

        public static void Data(string m, string t, string s)
        {
            if (!settings.data || !debug) return;
            Debug.Log(Format("white", m, t, s));
        }

        public static void Data(string m, string t, string s, params object[] p)
        {
            if (!settings.data || !debug) return;
            Debug.LogFormat(Format("white", m, t, s), p);
        }

        public static void Info(string s)
        {
            if (!settings.info || !debug) return;
            Debug.Log(Format("#00D5FF", s));
        }

        public static void Info(string s, params object[] p)
        {
            if (!settings.info || !debug) return;
            Debug.LogFormat(Format("#00D5FF", s), p);
        }

        public static void Action(string s)
        {
            if (!settings.action || !debug) return;
            Debug.Log(Format("#FF0080", s));
        }

        public static void Action(string s, params object[] p)
        {
            if (!settings.action || !debug) return;
            Debug.LogFormat(Format("#FF0080", s), p);
        }

        public static void Action(string e, string s, params object[] p)
        {
            if (!settings.action || !debug) return;
            Debug.LogFormat(Format("#FF0080", e, s), p);
        }

        public static void Warn(string s)
        {
            if (!settings.warn || !debug) return;
            Debug.Log(Format("orange", s));
        }

        public static void Warn(string s, params object[] p)
        {
            if (!settings.warn || !debug) return;
            Debug.LogFormat(Format("orange", s), p);
        }

        public static void Error(string s)
        {
            if (!settings.error || !debug) return;
            Debug.Log(Format("red", s));
        }

        public static void Error(string s, params object[] p)
        {
            if (!settings.error || !debug) return;
            Debug.LogFormat(Format("red", s), p);
        }

        public static void Log(string s)
        {
            if (!debug) return;
            Debug.Log(Format("#FFD500", s));
        }

        public static void Log(string s, params object[] p)
        {
            if (!debug) return;
            Debug.LogFormat(Format("#FFD500", s), p);
        }

        public static void LogTime(string k, Action cb)
        {
            if (!debug) return;
            var startTime = Time.realtimeSinceStartup;
            cb();
            var elapsedTime = Time.realtimeSinceStartup - startTime;
            Info("{0}耗时：{1:F2} 毫秒", k, elapsedTime * 1000);
        }
    }
}